<?php

/**
 * This is the model class for table "order".
 *
 * The followings are the available columns in table 'order':
 * @property string $order_id
 * @property integer $order_type
 * @property string $code
 * @property string $distributor_id
 * @property string $user_id
 * @property integer $product_id
 * @property integer $delivery_address_id
 * @property integer $billing_address_id
 * @property integer $payment_method
 * @property integer $transporter_method
 * @property integer $amount
 * @property double $shipping_price
 * @property double $shipping_tax
 * @property double $buying_price
 * @property double $buy_tax
 * @property double $total_price
 * @property integer $status
 * @property integer $created_time
 * @property integer $cancelled_time
 * @property integer $cancelled_by
 * @property integer $modified_time
 * @property integer $modified_by
 * @property string $comment
 */
class Order extends CActiveRecord
{
    public static $STATUS_WAITFORCHECK = 1;
    public static $STATUS_DISTRIBUTORCHECKED = 2;
    public static $STATUS_WAITFORLIQUIDATE = 3;
    public static $STATUS_LIQUIDATED = 4;
	/**
	 * Returns the static model of the specified AR class.
	 * @return Order the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'order';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('distributor_id, code, product_id, delivery_address_id, billing_address_id, payment_method, transporter_method, amount, status, created_time', 'required'),
			array('order_type, product_id, delivery_address_id, billing_address_id, payment_method, transporter_method, amount, status, created_time, cancelled_time, cancelled_by, modified_time, modified_by', 'numerical', 'integerOnly'=>true),
			array('shipping_price, shipping_tax, buying_price, buy_tax, total_price', 'numerical'),
			array('distributor_id, user_id', 'length', 'max'=>10),
            array('code', 'length', 'max'=>255),
			array('comment', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('order_id, order_type, distributor_id, user_id, product_id, delivery_address_id, billing_address_id, payment_method, transporter_method, amount, shipping_price, shipping_tax, buying_price, buy_tax, total_price, status, created_time, cancelled_time, cancelled_by, modified_time, modified_by, comment', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'customer' => array(self::BELONGS_TO, 'Customer', 'user_id'),
			'address' => array(self::BELONGS_TO, 'Address', 'address_id'),
			'billingAddress' => array(self::BELONGS_TO, 'BillingAddress', 'billing_address_id'),
			'deliveryAddress' => array(self::BELONGS_TO, 'DeliveryAddress', 'delivery_address_id'),
			'paymentMethod' => array(self::BELONGS_TO, 'PaymentMethod', 'payment_method'),
			'shippingMethod' => array(self::BELONGS_TO, 'TransporterMethod', 'transporter_method'),


		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'order_id' => Shop::t('Order'),
			'order_type' => 'Order Type',
            'code' => 'Code',
			'distributor_id' => Shop::t('Distributor'),
			'user_id' => 'User',
			'product_id' => 'Product',
			'delivery_address_id' => 'Delivery Address',
			'billing_address_id' => 'Billing Address',
			'payment_method' => 'Payment Method',
			'transporter_method' => 'Transporter Method',
			'amount' => 'Amount',
			'shipping_price' => Shop::t('Shipping Price'),
			'shipping_tax' => Shop::t('Shipping Tax'),
			'buying_price' => Shop::t('Buying Price'),
			'buy_tax' => 'Buy Tax',
			'total_price' => 'Total Price',
			'status' => Shop::t('Status'),
			'created_time' => Shop::t('Created Time'),
			'cancelled_time' => 'Cancelled Time',
			'cancelled_by' => 'Cancelled By',
			'modified_time' => 'Modified Time',
			'modified_by' => 'Modified By',
			'comment' => 'Comment',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('order_id',$this->order_id,true);
		$criteria->compare('order_type',$this->order_type);
        $criteria->compare('code',$this->code,true);
		$criteria->compare('distributor_id',$this->distributor_id,true);
		$criteria->compare('email',$this->user_id,true);
		$criteria->compare('name',$this->product_id);
		$criteria->compare('delivery_address_id',$this->delivery_address_id);
		$criteria->compare('billing_address_id',$this->billing_address_id);
		$criteria->compare('payment_method',$this->payment_method);
		$criteria->compare('transporter_method',$this->transporter_method);
		$criteria->compare('amount',$this->amount);
		$criteria->compare('shipping_price',$this->shipping_price);
		$criteria->compare('shipping_tax',$this->shipping_tax);
		$criteria->compare('buying_price',$this->buying_price);
		$criteria->compare('buy_tax',$this->buy_tax);
		$criteria->compare('total_price',$this->total_price);
		$criteria->compare('status',$this->status);
		$criteria->compare('created_time',$this->created_time);
		$criteria->compare('cancelled_time',$this->cancelled_time);
		$criteria->compare('cancelled_by',$this->cancelled_by);
		$criteria->compare('modified_time',$this->modified_time);
		$criteria->compare('modified_by',$this->modified_by);
		$criteria->compare('comment',$this->comment,true);
        $criteria->join = 'INNER JOIN customer ON customer.customer_id = t.user_id';
		$model = new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
        return $model;
	}
    
    public function search1()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('order_id',$this->order_id,true);
		$criteria->compare('order_type',$this->order_type);
        $criteria->compare('code',$this->code,true);
		$criteria->compare('distributor_id',$this->distributor_id,true);
		$criteria->compare('user_id',$this->user_id,true);
		$criteria->compare('name',$this->product_id);
		$criteria->compare('delivery_address_id',$this->delivery_address_id);
		$criteria->compare('billing_address_id',$this->billing_address_id);
		$criteria->compare('payment_method',$this->payment_method);
		$criteria->compare('transporter_method',$this->transporter_method);
		$criteria->compare('amount',$this->amount);
		$criteria->compare('shipping_price',$this->shipping_price);
		$criteria->compare('shipping_tax',$this->shipping_tax);
		$criteria->compare('buying_price',$this->buying_price);
		$criteria->compare('buy_tax',$this->buy_tax);
		$criteria->compare('total_price',$this->total_price);
		$criteria->compare('status',$this->status);
		$criteria->compare('created_time',$this->created_time);
		$criteria->compare('cancelled_time',$this->cancelled_time);
		$criteria->compare('cancelled_by',$this->cancelled_by);
		$criteria->compare('modified_time',$this->modified_time);
		$criteria->compare('modified_by',$this->modified_by);
		$criteria->compare('comment',$this->comment,true);
        $criteria->join = 'INNER JOIN product ON product.id=product_id';
		$model = new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
        return $model;
	}
    protected function searchProduct(){
        $criteria = new CDbCriteria;
        $criteria->compare('name', $this->product_id);
        $criteria->join = 'INNER JOIN customer ON customer.customer_id = t.user_id';
        return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
            ));
    }
    
    public function searchByCustomer(){
        $criteria=new CDbCriteria;

		$criteria->compare('order_id',$this->order_id,true);
		$criteria->compare('order_type',$this->order_type);
        $criteria->compare('code',$this->code,true);
		$criteria->compare('distributor_id',$this->distributor_id,true);
		$criteria->compare('user_id',$this->user_id,true);
		$criteria->compare('name',$this->product_id);
		$criteria->compare('delivery_address_id',$this->delivery_address_id);
		$criteria->compare('billing_address_id',$this->billing_address_id);
		$criteria->compare('payment_method',$this->payment_method);
		$criteria->compare('transporter_method',$this->transporter_method);
		$criteria->compare('amount',$this->amount);
		$criteria->compare('shipping_price',$this->shipping_price);
		$criteria->compare('shipping_tax',$this->shipping_tax);
		$criteria->compare('buying_price',$this->buying_price);
		$criteria->compare('buy_tax',$this->buy_tax);
		$criteria->compare('total_price',$this->total_price);
		$criteria->compare('status',$this->status);
		$criteria->compare('created_time',$this->created_time);
		$criteria->compare('cancelled_time',$this->cancelled_time);
		$criteria->compare('cancelled_by',$this->cancelled_by);
		$criteria->compare('modified_time',$this->modified_time);
		$criteria->compare('modified_by',$this->modified_by);
		$criteria->compare('comment',$this->comment,true);
		$model = new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
        return $model;
    }
    
    public function getUser(){
        $customer = Customer::model()->findByPk($this->user_id);
        return User::model()->findByPk($customer->user_id);
    }
    public function getCustomer(){
        return Customer::model()->findByPk($this->user_id);
    }
    public function getProduct(){
        return Product::model()->findByPk($this->product_id);
    }
    
    public function getTotalPrice(){
        return $this->buy_tax + $this->buying_price + $this->shipping_price + $this->shipping_tax;
    }
    
    public function getStatusName(){
        switch ($this->status){
            case 1: 
                return "Chờ kiểm hàng";
            case 2:
                return "Đã kiểm tra";
            case 3:
                return "Chờ thanh toán";
            case 4: 
                return "Đã thanh toán";
            case 5:
                return "Đã nhận hàng";
        }
    }
    
    public function getOrderTransporter(){
        return OrderTransporter::model()->findByAttributes(array('order_id' => $this->order_id));
    }
}